#include "elapsed_timer.h"
#include "nv_utils.h"

_TRT_INFER_BEGIN
ElapsedTimer::ElapsedTimer()
{
    checkRuntime(cudaEventCreate((cudaEvent_t *)&start_));
    checkRuntime(cudaEventCreate((cudaEvent_t *)&stop_));
}

ElapsedTimer::~ElapsedTimer()
{
    checkRuntime(cudaEventDestroy((cudaEvent_t)start_));
    checkRuntime(cudaEventDestroy((cudaEvent_t)stop_));
}

void ElapsedTimer::start(void *stream)
{
    stream_ = stream;
    checkRuntime(cudaEventRecord((cudaEvent_t)start_), (cudaStream_t)stream_);
}

float ElapsedTimer::stop(void *stream)
{
    checkRuntime(cudaEventRecord((cudaEvent_t)stop_, (cudaStream_t)stream_));
    checkRuntime(cudaEventSynchronize((cudaEvent_t)stop_));

    float latency = 0.0f;
    checkRuntime(cudaEventElapsedTime(&latency, (cudaEvent_t)start_, (cudaEvent_t)stop_));
    return latency;
}




_TRT_INFER_END
